package com.xc.utils.ip;

import com.xc.utils.HttpRequest;
import com.xc.utils.PropertiesUtil;
import com.xc.utils.ip.juhe.AddressResultsVo;
import com.xc.utils.redis.JsonUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xiongcan
 * @version 1.0
 * @date 2018/9/24 下午7:35
 */
@Slf4j
public class JuheIpApi {

    //  请求地址
    private static final String ip_url = "http://apis.juhe.cn/ip/ip2addr";
    //配置您申请的KEY
    public static final String APPKEY = PropertiesUtil.getProperty("juhe.ip.key");


    //  ip地址转换成 地址
    public static String ip2Add(String ips){
        String params = "?ip=" + ips + "&key=" + APPKEY;

        String retStr = "";
        String address = "查不到此IP";
        if(!"0:0:0:0:0:0:0:1".equals(ips)){
            try {
                retStr = HttpRequest.doGet(ip_url,params);

                AddressResultsVo addressResultsVo = JsonUtil
                        .string2Obj(retStr,AddressResultsVo.class);

                address = addressResultsVo.getResult().getArea();
            }catch (Exception e){
                log.error("ip转换成地址发生异常,e={}",e);
            }
        }
        return address;
    }


}
